package com.example.mymvvmdemo.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.utils.LogUtils;
import com.example.mymvvmdemo.utils.StrUtils;


/**
 * 加载dialog
 */
public class LoadingAnimDialog {

    private LottieAnimationView joinLoad;
    private TextView tvMsg;
    private Dialog mLoadingDialog;

    public LoadingAnimDialog(Context context, String msg) {
        // 首先得到整个View
        View view = LayoutInflater.from(context).inflate(R.layout.view_anim_load, null);
        // 获取整个布局
        tvMsg = view.findViewById(R.id.tv_msg);
        // 页面中的LoadingView
        joinLoad = view.findViewById(R.id.la_load);
        if (StrUtils.isEmpty(msg)) {
            tvMsg.setText(context.getString(R.string.load));
        } else {
            tvMsg.setText(msg);
        }
        // 页面中显示文本
        // 创建自定义样式的Dialog
        mLoadingDialog = new Dialog(context, R.style.loading_dialog);
        // 设置返回键无效--true有效 false无效
        mLoadingDialog.setCancelable(false);

//        mLoadingDialog = new Dialog(context, R.style.DarkBackgroundDialog);
        mLoadingDialog.setContentView(view);
        Window dialogWindow = mLoadingDialog.getWindow();//获取Dialog的窗口
        //实例化窗口布局对象
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;//宽度铺满
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;//高度自适应
        lp.gravity = Gravity.CENTER;//窗口停靠在底部居中
        dialogWindow.setAttributes(lp);

    }

    public void setText(String msg) {
        tvMsg.setText(msg);
    }

    public void showText() {
        tvMsg.setVisibility(View.VISIBLE);
    }

    public void dismissText() {
        tvMsg.setVisibility(View.GONE);
    }

    public void show(Activity activity) {
        try {
            if (mLoadingDialog != null && !mLoadingDialog.isShowing()) {
                mLoadingDialog.show();
                joinLoad.playAnimation();

                WindowManager.LayoutParams attributes = activity.getWindow().getAttributes();
                attributes.alpha = 0.7f;
                activity.getWindow().setAttributes(attributes);
            }
        } catch (Exception e) {
            LogUtils.e("load dialog show error:" + e.getMessage());
        }
    }

    public void close(Activity activity) {
        try {
            if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
                WindowManager.LayoutParams attributes = activity.getWindow().getAttributes();
                attributes.alpha = 1.0f;
                activity.getWindow().setAttributes(attributes);

                joinLoad.cancelAnimation();
                mLoadingDialog.dismiss();
            }
        } catch (Exception e) {
            LogUtils.e("load dialog close error:" + e.getMessage());
        }
    }

    public void destroyDialog() {
        try {
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog.cancel();
                mLoadingDialog = null;
            }
        } catch (Exception e) {
            LogUtils.e(e.getMessage());
        }
    }

    public boolean isShowing() {
        if (mLoadingDialog != null) {
            boolean showing = mLoadingDialog.isShowing();
            return showing;
        }
        return false;
    }
}
